using SecondDemo.API.Filters;
using SecondDemo.API.Models;
using SecondDemo.API.Services;
using SecondDemo.API.Services.IService;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCors(c=>c.AddPolicy("any",p=>
    p.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));  // 添加一条策略  策略名为any

builder.Services.AddTransient<IUserService,UserService>();

builder.Services.AddTransient<IIocService1,IocService1>();
builder.Services.AddTransient<IIocService2,IocService2>();

builder.Services.AddScoped<IScopedService,ScopedService>();
builder.Services.AddTransient<ITransientService,TransientService>();
builder.Services.AddSingleton<ISingletonService,SingletonService>();

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddControllers(o=>{
    o.Filters.Add<CustomActionFilterAttribute>(1);  // 添加过滤器并指定顺序
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseCors();  // 应用跨域策略

app.UseAuthorization();

app.MapControllers();

// app.MapGet("/",()=>{
//     return "Eternum";
// });

app.MapPost("/",(TestPostEditViewModel test)=>{
    return test;
});
string Hi()=> "Welcome to Eternum";
app.MapPost("/hello",Hi);

app.Run();
